package com.im.handler;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.codec.http.DefaultFullHttpResponse;
import io.netty.handler.codec.http.FullHttpResponse;
import io.netty.handler.codec.http.HttpHeaderNames;
import io.netty.handler.codec.http.HttpObject;
import io.netty.handler.codec.http.HttpRequest;
import io.netty.handler.codec.http.HttpResponseStatus;
import io.netty.handler.codec.http.HttpVersion;
import io.netty.util.CharsetUtil;

/**
 * 自己处理的ChannerlHandler
 */

public class CustomerHandler extends SimpleChannelInboundHandler<HttpObject> {

  /**
   * 注册 channel
   * @param ctx
   * @throws Exception
   */
  @Override
  public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
    System.out.println("channel 注册");
    super.channelRegistered(ctx);
  }

  @Override
  public void channelUnregistered(ChannelHandlerContext ctx) throws Exception {
    System.out.println("channel 注销");
    super.channelUnregistered(ctx);
  }


  @Override
  public void channelActive(ChannelHandlerContext ctx) throws Exception {
    System.out.println("channel 活跃状态");
    super.channelActive(ctx);
  }

  @Override
  public void channelInactive(ChannelHandlerContext ctx) throws Exception {
    System.out.println("channel 与服务器断开连接");
    super.channelInactive(ctx);
  }


  @Override
  public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
    System.out.println("channel 读取数据完毕");
    super.channelReadComplete(ctx);
  }

  @Override
  public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
    System.out.println("用户事件触发");
    super.userEventTriggered(ctx, evt);
  }

  @Override
  public void channelWritabilityChanged(ChannelHandlerContext ctx) throws Exception {
    System.out.println("channel 可写事件更改");
    super.channelWritabilityChanged(ctx);
  }

  @Override
  public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
    System.out.println("channel 异常捕获");
    super.exceptionCaught(ctx, cause);
  }

  @Override
  public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
    System.out.println("handler类添加");
    super.handlerAdded(ctx);
  }

  @Override
  public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
    System.out.println("handler类移除");
    super.handlerRemoved(ctx);
  }


  @Override
  protected void channelRead0(ChannelHandlerContext channelHandlerContext, HttpObject httpObject)
      throws Exception {
    //获取链接
    Channel channel = channelHandlerContext.channel();
    if (httpObject instanceof HttpRequest) { //如果接受的数据是请求
      System.out.println("远程客户端地址"+channel.remoteAddress());
      ByteBuf byteBuf = Unpooled.copiedBuffer("hello world", CharsetUtil.UTF_8);
      //构建一个HttpResponse，响应客户端
      FullHttpResponse response =
          /**
           * params1:针对Http的版本号
           * params2:状态（响应成功或失败）
           * params3:内容
           */
          //HttpVersion.HTTP_1_1：默认开启keep-alive
          new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, byteBuf);
      //设置当前内容长度、类型等
      response.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/plain");
      //readableBytes：可读长度
      response.headers().set(HttpHeaderNames.CONTENT_LENGTH, byteBuf.readableBytes());
      channel.writeAndFlush(response);
    }
  }
}
